package com.yxzr.youyou.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.yxzr.youyou.R;
import com.yxzr.youyou.VideoDetailActivity;
import com.yxzr.youyou.bean.TwoTypeResBean;
import com.yxzr.youyou.retrofit.DataUrl;

import java.util.ArrayList;
import java.util.List;

public class ComicRecyclerViewAdapter extends RecyclerView.Adapter<ComicRecyclerViewAdapter.ViewHolder> {
    private Context mContext;
    private List<TwoTypeResBean.DataBean> list = new ArrayList<>();
    private int index = 0;
    public ComicRecyclerViewAdapter(Context context,int index){
        mContext = context;
        this.index = index;
    }

    public void addList(List<TwoTypeResBean.DataBean> mlist){
        if (mlist != null && mlist.size() > 0){
            list.addAll(mlist);
            notifyDataSetChanged();
        }
    }

    public void clearList(){
        list.clear();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewHolder viewHolder = new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.movie_adapter_layout,parent,false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        if (TextUtils.isEmpty(list.get(position).getPath())){
            Glide.with(mContext)
                    .setDefaultRequestOptions(
                            new RequestOptions()
                                    .frame(3000000)
                                    .centerCrop()
                                    .error(R.drawable.dialog_loading)
                                    .placeholder(R.drawable.dialog_loading))
                    .load(list.get(position).getUrl())
                    .into(holder.movie_img);
        }else {
            Glide.with(mContext)
                    .setDefaultRequestOptions(
                            new RequestOptions()
                                    .frame(3000000)
                                    .centerCrop()
                                    .error(R.drawable.dialog_loading)
                                    .placeholder(R.drawable.dialog_loading))
                    .load(DataUrl.imgUrl+list.get(position).getPath())
                    .into(holder.movie_img);
        }


        holder.movie_tv_serialize.setText(list.get(position).getSerialize());
        holder.movie_tv_title.setText(list.get(position).getTitle());

        holder.movie_rel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, VideoDetailActivity.class);
                intent.putExtra("id",list.get(position).getId());
                intent.putExtra("index",index);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {

        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView movie_tv_serialize,movie_tv_title;
        private ImageView movie_img;
        private LinearLayout movie_rel;
        public ViewHolder(View itemView) {
            super(itemView);
            movie_tv_serialize = itemView.findViewById(R.id.movie_tv_serialize);
            movie_tv_title = itemView.findViewById(R.id.movie_tv_title);
            movie_img = itemView.findViewById(R.id.movie_img);
            movie_rel = itemView.findViewById(R.id.movie_rel);
        }
    }
}
